
<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">通用大数据框架智能计算引擎优化仿真平台</el-breadcrumb-item>
      <el-breadcrumb-item>docker</el-breadcrumb-item>
      <el-breadcrumb-item>容器创建</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="15">
        <el-col :span="8">
          <el-input
              v-model="keywords" style="width: 200px" prefix-icon="el-icon-search" placeholder="请输入容器名称" />
        </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="addOptimizeSchemeVisble = true" >创建容器</el-button>
        </el-col>
      </el-row>

      <el-table
          :data="optimizeSchemeList.filter(data => !keywords || data.container_name.toLowerCase().includes(keywords.toLowerCase()))"
          border
          style="width: 100%"
          :key="mainTableKey">
        <el-table-column
            prop="course_id"
            label="课程id"
            width="170">
        </el-table-column>
        <el-table-column
            prop="course_name"
            label="课程名字"
            width="170">
        </el-table-column>        <el-table-column
          prop="course_schools"
          label="主办学院"
          width="170">
      </el-table-column>        <el-table-column
          prop="course_name"
          label="课程名字"
          width="170">
      </el-table-column>
        <el-table-column
            prop="operate"
            label="操作"
            width="150">
          <template v-slot="scope">
            <el-tooltip class="item" effect="dark" content="编辑dockers" placement="top">
              <el-button type="primary" icon="el-icon-edit" @click="editOptimizeScheme(scope.row)" ></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除dockers" placement="top">
              <el-button type="danger" icon="el-icon-delete" @click="removeOptimizeScheme(scope.$index)"
                        ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

<!--      <div class="resource-page">-->
<!--        <el-pagination @size-change="SizeChange" @current-change="CurrentChange"-->
<!--                       :page-sizes="[3,5,15]" :page-size="pagesize" :pager-count="10"-->
<!--                       layout=" prev, pager, next,sizes, jumper" :total="filterDataShow.length">-->
<!--        </el-pagination>-->
<!--      </div>-->

    </el-card>
    <el-dialog
        title="编辑docker"
        :visible.sync="editOptimizeSchemeVisble"
        width="30%"
        @close="closeeditOptimizeSchemeDialog"
    >

      <el-form>

        <el-input placeholder="修改容器ID" v-model="inputID" ></el-input>

        <el-form-item label="容器状态 :">
          <el-select v-model="status1" placeholder="请选择">
            <el-option
                v-for="item in status"
                :key="item.status1"
                :label="item.label4"
                :value="item.status1">
            </el-option>
          </el-select>
        </el-form-item>
        <el-input placeholder="修改容器名字" v-model="inputName" ></el-input>
        <el-input placeholder="修改容器CREATED" v-model="inputcreated" ></el-input>
        <el-form-item label="镜像 :">
          <el-select v-model="value" placeholder="请选择">
            <el-option
                v-for="item in image"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="容器分类 :">
          <el-select v-model="value" placeholder="请选择">
            <el-option
                v-for="item in type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
     <el-button @click="editOptimizeSchemeVisble = false">取 消</el-button>
     <el-button type="primary" @click="editOptimizeSchemeList">确 定</el-button>
     </span>
    </el-dialog>

    <el-dialog
        title="创建容器"
        :visible.sync="addOptimizeSchemeVisble"
        width="30%"
    >
      <el-form>
        <el-form-item label="容器名字:">
          <el-input placeholder="推荐值：nginx-wsl" v-model="create_container.container_name" ></el-input>
        </el-form-item>
        <el-form-item label="容器类型:">
          <el-input placeholder="推荐值：nginx" v-model="create_container.container_type"></el-input>
        </el-form-item>
        <el-form-item label="容器数量:">
          <el-input placeholder="推荐值:大于0" v-model="create_container.container_number" ></el-input>
        </el-form-item>
        <el-form-item label="容器命名空间:">
          <el-input placeholder="推荐值：bigdata-platform" v-model="create_container.container_namespace" ></el-input>
        </el-form-item>
        <el-form-item label="cpu:">
          <el-input placeholder="推荐值：100" v-model="create_container.container_cpu" ></el-input>
        </el-form-item>
        <el-form-item label="内存量:">
          <el-input placeholder="推荐值：50-150" v-model="create_container.container_mem" ></el-input>
        </el-form-item>
        <el-form-item label="容器路径:">
          <el-input placeholder="推荐值：/home/bigdata_platform/docker_data" v-model="create_container.container_volume" ></el-input>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="add_container">确 定</el-button>
     <el-button @click="addOptimizeSchemeVisble = false">取 消</el-button>

     </span>
    </el-dialog>

  </div>
</template>
<script>
import {add_container, del_container, find_all_container} from "@/api/file";
export default {
  data(){
    return{
      status1:'',
      inputcreated:'',
      inputName:'',
      inputID:'',
      input:'',
      cpu1:'',
      mem1:'',
      volume:'',
      // 删除容器传给后端的值
      remove_container:{
        container_name:'',  // 处理器名字
        container_namespace:'bigdata-platform',  //容器命名空间
      },
      // 输入框获取的值
      create_container:{
        container_name:'nginx-wsl-test',  // 处理器名字
        container_type:'nginx',  // 容器类型
        container_number:2, // 容器数量
        container_namespace:'bigdata-platform',  //容器命名空间
        container_cpu:100,  // cpu核
        container_mem:256,  // 内存量
        container_volume:'/home/bigdata_platform/docker_data'  //  容器路径
      },
      test:[
        {
          container_name:'nginx-wsl1',  // 处理器名字
          container_type:'nginx',  // 容器类型
          container_number:'2', // 容器数量
          container_namespace:'bigdata-platform',  //容器命名空间
          container_cpu:'100',  // cpu核
          container_mem:'256',  // 内存量
          container_volume:'/home/bigdata_platform/docker_data'  //  容器大小
        },{
          container_name:'nginx-wsl1',  // 处理器名字
          container_type:'nginx',  // 容器类型
          container_number:'2', // 容器数量
          container_namespace:'bigdata-platform',  //容器命名空间
          container_cpu:'100',  // cpu核
          container_mem:'256',  // 内存量
          container_volume:'/home/bigdata_platform/docker_data'  //  容器大小
        }

      ],

      queryInfo:{
        //查询参数
        query:'',
        pagenum:1,
        pagesize:10
      },
      total:0,
      optimizeSchemeList:[],
      mainTableKey: 1,
      editOptimizeSchemeParams: {

      },
      editOptimizeSchemeVisble : false,
      addOptimizeSchemeVisble : false,
      addOptimizeSchemefromlocalVisble:false,
      namespace: {
        container_namespace:'bigdata-platform',
      },
      // 表格分页+ 搜索
      // currentPage:1,
      // total:200,
      // pagesize:10,
      // filterDataShow: [], //储存符合条件的数据

      keywords:'',
    }
  },
  methods:{
    closeeditOptimizeSchemeDialog(){
      this.$refs.editOptimizeSchemeFormRef.resetFields()
    },
    getoptimizeSchemeList() {
      find_all_container(this.namespace).then(res=>{
        this.optimizeSchemeList = res
      })
    },
    editOptimizeScheme(row){
      this.editOptimizeSchemeParams = row
      this.editOptimizeSchemeVisble = true
    },
    editOptimizeSchemeList(){
      this.$http.put(`/${this.editOptimizeSchemeParams.containerid}`,this. editOptimizeSchemeParams).then(res=>{
        if(res.data.meta.status !== 200) return this.$message.error('更新失败')
        this.getoptimizeSchemeList()
      })
    },
    removeOptimizeScheme(index){
      this.$confirm('此操作将永久删除'+index.container_name+', 是否继续?', '删除优化方案', {
        confirmButtonText: 'determine',
        cancelButtonText: 'cancel',
        type: 'warning'
      }).then(() => {
        this.remove_container.container_name = this.optimizeSchemeList[index].container_name
        del_container(this.remove_container)

        this.optimizeSchemeList.splice(index,1)  // 在前端页面删除
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    add_container(){
      add_container(this.create_container).then(res=>{
        if(res.toString() == "Created successfully"){
          this.success_tip()
          this.optimizeSchemeList.push(this.create_container)
        }else{
          this.fail_tip()
        }
        // 逻辑处理
      }).catch(err=>{
        console.log(err)
        this.$message.error(err);
      })
    },
    success_tip() {
      this.$message({
        message: '创建成功',
        type: 'success'
      });
    },
    fail_tip(){
      this.$message({
        message: '创建失败',
        type: 'fail'
      });
    }

    // 表格分页
    // current_change:function(currentPage){
    //   this.currentPage = currentPage;
    // },
  },

  created(){
    console.log("created")
    this.getoptimizeSchemeList()
  },

}
</script>
<style>

</style>
